package words;

import java.io.*;


public class IntexDocument {

  int m_nSents;
  IntexSentence[] m_sents;

  public IntexDocument(SgmlTokSentDocument sgmlTSDoc, String strTag)
  {
    SgmlTokSentence[] sents = sgmlTSDoc.getSentences();
    m_nSents = sents.length;

    m_sents = new IntexSentence[m_nSents];
    for (int i = 0; i < sents.length; i++)
      m_sents[i] = new IntexSentence(sents[i], strTag);
  }

  public int[] pairsCounts()
  {
    int[] counts = new int[2];
    for (int i = 0; i < m_nSents; i++) {
      int[] sCounts = m_sents[i].pairsCounts();
      for (int j = 0; j < counts.length; j++)
	counts[j] += sCounts[j];
    }

    return counts;
  }

  public boolean writeInf(String strFile)
  {
    try {
      PrintWriter pw = new PrintWriter(new FileWriter(strFile));
      int crtPairs = 0;
      for (int i = 0; i < m_sents.length; i++)
	crtPairs += m_sents[i].writeInf(pw, crtPairs);
      pw.close();
    }
    catch (IOException e) {
      System.err.println(e);
      return false;
    }

    return true;
  }


  public boolean writeACE(String strDir, String strDocID)
  {
    StringBuffer strText = new StringBuffer();
    StringBuffer strEnts = new StringBuffer();
    StringBuffer strRels = new StringBuffer();
    int[] counts = {0, 0};
    int nHeader = 1 + strDocID.length() + 1 + 1 + 8 + 1 + 5;
    
    for (int i = 0; i < m_sents.length; i++)
      m_sents[i].writeACE(strText, strEnts, strRels, counts, nHeader);

    try {
      PrintWriter pwText = new PrintWriter(new FileWriter(strDir + "/" + 
							  strDocID + ".sgm"));
      pwText.println("<DOC>");
      pwText.println("<DOCNO> " + strDocID + " </DOCNO>");
      pwText.println("<DOCTYPE SOURCE=\"Medline\"> Abstract </DOCTYPE>");
      pwText.println("<HEADER></HEADER>");
      pwText.println("<TEXT>");
      
      pwText.println(strText);

      pwText.println("</TEXT>");
      pwText.println("</DOC>");
      pwText.close();

      PrintWriter pwAnno = new PrintWriter(new FileWriter(strDir + "/" + 
							  strDocID + ".xml"));
      pwAnno.println("<?xml version=\"1.0\"?>");
      pwAnno.println("<!DOCTYPE source_file SYSTEM \"./ace-rdc.v2.0.1.dtd\">");
      pwAnno.println("<source_file TYPE=\"text\" VERSION=\"2.0\" SOURCE=\"Medline\" URI=\"" + strDocID + ".sgm\">");
      pwAnno.println("<document DOCID=\"" + strDocID + "\">");
      pwAnno.println();

      pwAnno.println(strEnts);
      pwAnno.println(strRels);

      pwAnno.println("</document>");
      pwAnno.println("</source_file>");
      pwAnno.close();
    }
    catch (IOException e) {
      System.err.println(e);
      return false;
    }

    return true;
  }


  /* main test method */
  static public void main(String[] args)
  {
    if (args.length != 4) {
      System.out.println("Usage: java IntexDocument <corpus-list> " +
			 " <tag-list> <input-dir> <output-dir>!");
      System.exit(1);
    }

    TagDefinition tagDef = new TagDefinition();
    tagDef.load(args[1]);

    try {
      BufferedReader br = new BufferedReader(new FileReader(args[0]));
      String strLine = br.readLine();
      while (strLine != null) {
        strLine = strLine.trim();
        if (strLine.length() > 0) {
          // load input document
          String strInFile = args[2] + "/" + strLine + ".out";
          System.out.print("processing [" + strLine + "] ... ");

	  SgmlDocument sgmlDoc = new SgmlDocument(tagDef);
	  sgmlDoc.load(strInFile);
	  SgmlTokSentDocument sgmlTSDoc = new SgmlTokSentDocument(sgmlDoc);
	  IntexDocument ixDoc = new IntexDocument(sgmlTSDoc, "prot");
	  ixDoc.writeACE(args[3], strLine);

          System.out.println("done!");
        }
        strLine = br.readLine();
      }
    }
    catch (IOException e) {
      System.err.println(e);
      System.exit(1);
    }
  }
  
}
